<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xmi="http://schema.omg.org/spec/XMI/2.1"
    xmlns:MASC_Profile="http:///schemas/MASC_Profile/_OnYS4BTgEd6yO-ZURVEtWw/6"
    xmlns:masc-config="http://www.nicta.com.au/projects/armature/masc-config"
    xmlns:masc-se="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-se"
    exclude-result-prefixes="xs" version="2.0">
    
<xsl:variable name="config" select="document('config.xml')"/>
    <xsl:variable name="newline">
        <xsl:text>&#10;</xsl:text>
    </xsl:variable>
    <xsl:template name="t1" match="/">
        <!-- could add a for loop for multiple When-FilteringCondition -->
        <xsl:if test="/root/Element[1]/GPA[1]/When-FilteringCondition[1]/AND[1]/NotEqual[1]/MonitoredData[1]/@Name">
            <xsl:text>have name</xsl:text>
        </xsl:if>
        <xsl:if test="not(/root/Element[1]/GPA[1]/When-FilteringCondition[1]/AND[1]/NotEqual[1]/MonitoredData[1]/@Name)">
            <xsl:text>have no name</xsl:text>
        </xsl:if>
        
        <xsl:copy-of select="$config/masc-config:XMIFile/masc-config:UMLDiagram[1]/masc-config:UMLElement[3]/masc-se:Schedules[1]"></xsl:copy-of>
        
        <xsl:text disable-output-escaping="yes">&#x201d;</xsl:text>
        <xsl:text disable-output-escaping="yes">&quot;</xsl:text>
        <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
        <xsl:text disable-output-escaping="yes">&#60;</xsl:text>
    </xsl:template>

    <xsl:template name="substring-after-last">
        <xsl:param name="string" />
        <xsl:param name="delimiter" />
        <xsl:choose>
            <xsl:when test="contains($string, $delimiter)">
                <xsl:call-template name="substring-after-last">
                    <xsl:with-param name="string"
                        select="substring-after($string, $delimiter)" />
                    <xsl:with-param name="delimiter" select="$delimiter" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise><xsl:value-of select="$string" /></xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template name="substring-before-last">
        <!-- ab-cd-ef -->
        <xsl:param name="string"/>
        <xsl:param name="delimiter"/>
        <xsl:choose>
            <xsl:when test="$delimiter and contains($string, $delimiter)">
                <xsl:variable name="temp" select="substring-after($string, $delimiter)"/>    <!-- cd-ef -->
                <xsl:value-of select="substring-before($string, $delimiter)"/>    <!-- ab -->
                <xsl:choose>
                    <xsl:when test="contains($temp, $delimiter)">
                        <xsl:value-of select="$delimiter"/>
                        <xsl:call-template name="substring-before-last">
                            <xsl:with-param name="string" select="$temp"/>
                            <xsl:with-param name="delimiter" select="$delimiter"/>
                        </xsl:call-template>
                    </xsl:when>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise><xsl:value-of select="$string" /></xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template match="GPA/When-FilteringCondition">
        <xsl:value-of select="$newline"/>
        <xsl:text>I'm in filtering condition in GPA</xsl:text>
        <xsl:value-of select="$newline"/>
        <xsl:if test=".//Equal">
            <xsl:apply-templates select=".//Equal"/>
        </xsl:if>
        <xsl:if test=".//NotEqual">
            <xsl:apply-templates select=".//NotEqual"/>
        </xsl:if>

        <xsl:if test="./AND">
            <xsl:text>I'm in AND------</xsl:text>
            <xsl:value-of select="../../../Element"></xsl:value-of>
        </xsl:if>
        <xsl:if test="./OR">
            <xsl:apply-templates select="./OR"/>
        </xsl:if>
    </xsl:template>
    
    <xsl:template match="MD/When-FilteringCondition">
        <xsl:value-of select="$newline"/>
        <xsl:text>I'm in filtering condition in MD</xsl:text>
        <xsl:value-of select="$newline"/>
        <xsl:if test=".//Equal">
            <xsl:apply-templates select=".//Equal"/>
        </xsl:if>
        <xsl:if test=".//NotEqual">
            <xsl:apply-templates select=".//NotEqual"/>
        </xsl:if>
        
        <xsl:if test="./AND">
            <xsl:apply-templates select="./AND"/>
        </xsl:if>
        <xsl:if test="./OR">
            <xsl:apply-templates select="./OR"/>
        </xsl:if>
    </xsl:template>

    <xsl:template match="GPA/When-FilteringCondition//NotEqual">
        <xsl:text>I'm NotEqual</xsl:text>
        <xsl:value-of select="$newline"/>
        <xsl:value-of select="./AwUConst/@Value"/>
        <xsl:value-of select="$newline"/>
    </xsl:template>

    <xsl:template match="GPA/When-FilteringCondition//Equal">
        <xsl:text>I'm Equal</xsl:text>
        <xsl:value-of select="$newline"/>
        <xsl:value-of select="./AwUConst/@Value"/>
        <xsl:value-of select="$newline"/>
    </xsl:template>

    <xsl:template match="GPA/When-FilteringCondition/AND">
        <xsl:text>I'm AND</xsl:text>
    </xsl:template>

    <xsl:template match="GPA/When-FilteringCondition/OR">
        <xsl:text>I'm OR</xsl:text>
    </xsl:template>
    
    <xsl:template match="MD/When-FilteringCondition//NotEqual">
        <xsl:text>I'm NotEqual in MD</xsl:text>
        <xsl:value-of select="$newline"/>
        <xsl:value-of select="./AwUConst/@Value"/>
        <xsl:value-of select="$newline"/>
    </xsl:template>
    
    <xsl:template match="MD/When-FilteringCondition//Equal">
        <xsl:text>I'm Equal</xsl:text>
        <xsl:value-of select="$newline"/>
        <xsl:value-of select="./AwUConst/@Value"/>
        <xsl:value-of select="$newline"/>
    </xsl:template>
    
    <xsl:template match="MD/When-FilteringCondition/AND">
        <xsl:text>I'm AND</xsl:text>
    </xsl:template>
    
    <xsl:template match="MD/When-FilteringCondition/OR">
        <xsl:text>I'm OR</xsl:text>
    </xsl:template>

</xsl:stylesheet>
